package com.example.jpa.demo_jpa.controller;

import com.example.jpa.demo_jpa.model.AyUser;
import com.example.jpa.demo_jpa.model.PhoneType;
import com.example.jpa.demo_jpa.service.AyUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AyUserController {
    @Resource
    private AyUserService ayUserService;

    //查看所有
    @RequestMapping("/sar")
    public String phone_type(Model model){
        List<AyUser> a  = ayUserService.findAll();
        model.addAttribute("search_result",a);
        return "ayuser/userList";
    }

    @RequestMapping(value = "/search") //搜索时跳转的路由
    public String search(HttpServletRequest request, HttpSession httpSession, Model model) {
        //获取搜索框输入
        String search_input = request.getParameter("searchName");
        List<AyUser> tmp = ayUserService.findByName(search_input);//调用对应服务层函数获取返回的搜索结果list集合 tmp
        System.out.println(tmp); //在控制台输出检查是否争取返回
        if (tmp != null) {//返回不为空。搜索成功
            /*将搜索结果集合、集合元素个数(结果商品个数)、搜索关键字添加到model的属性中返回前端页面*/
            model.addAttribute("search_result", tmp);//搜索结果商品list
            return "ayuser/userList";//跳转到搜索页search.html
        } else {//搜索失败
            model.addAttribute("error_search_fail", "没有相关信息");
            return "ayuser/userList";
        }
    }


}
